﻿(function($) {
    var _$form = $('form[name=EditForm]');
    _$form.validate({
        success: function (label, element) {
            // Add the span element, if doesn't exists, and apply the icon classes to it.
            if (!$(element).next("span")[0]) {
                $("<span class='form-control-feedback fa fa-check '></span>").insertAfter($(element));
            }
        },
        highlight: function (element, errorClass, validClass) {
            $(element).parents(".form-group").addClass("has-error").removeClass("has-success");

            if ($(element).parent().find(".form-control-feedback").length === 0) {
                $("<span class='form-control-feedback fa fa-remove '></span>").insertAfter($(element));
            } else {
                $(element).parent().find(".form-control-feedback").addClass("fa-remove").removeClass("fa-check");
            }

        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).parents(".form-group").addClass("has-success").removeClass("has-error");

            if ($(element).parent().find(".form-control-feedback").length === 0) {
                $("<span class='form-control-feedback fa fa-check '></span>").insertAfter($(element));
            } else {
                $(element).parent().find(".form-control-feedback").addClass("fa-check").removeClass("fa-remove");
            }
        }
        });

    _$form.find('input').on('keypress', function (e) {
        if (e.which === 13) {
            $(".save-button").click();
        }
     });

    $.AdminBSB.input.activate(_$form);

    $(".save-button").click(function(e) {
        e.preventDefault();

        if (!_$form.valid()) {
            return;
        }

      if (typeof saveFormEvent != "undefined") {
        if (typeof (eval(saveFormEvent)) == "function") {
          saveFormEvent(_$form);
        } else {
          abp.message.error("表单保存方法【saveFormEvent】未定义.");
        }
      } else {
        abp.message.error("表单保存方法【saveFormEvent】未定义.");
      }
    });

    $(".close-button").click(function (e) {
        DiyWindow.closeTabOrModal();

        if (typeof closeFormEvent != "undefined") {
          if (typeof (eval(closeFormEvent)) == "function") {
                closeFormEvent();
            } 
        }
    });

    //验证
    $("input[type='text']").each(function() {
        if ($(this).data("val") === true) {
            var maxLength = $(this).data("val-maxlength-max");
            var maxLengthMeg = $(this).data("val-maxlength");
            var minLength = $(this).data("val-minlength-min");
            var minLengthMeg = $(this).data("val-minlength");
            var strlengthMax = $(this).data("val-length-max");
            var strlengthMin = $(this).data("val-length-min");
            var strlengthMeg = $(this).data("val-length");
            var requidMeg = $(this).data("val-required");
            var mailMeg = $(this).data("val-email");
            var rangeMeg = $(this).data("val-range");
            var rangeMax = $(this).data("val-range-max");
            var rangeMin = $(this).data("val-range-min");
            var regex = $(this).data("val-regex-pattern");
            var regexMeg = $(this).data("val-regex");

            //字符串最大长度
            if (maxLength != undefined) {
                $(this).rules("add",
                    {
                        maxlength: maxLength,
                        messages: {
                            maxlength: maxLengthMeg
                        }
                    });
            }

            //字符串最小长度
            if (minLength != undefined) {
                $(this).rules("add",
                    {
                        minlength: minLength,
                        messages: {
                            minlength: minLengthMeg
                        }
                    });
            }

             //字符串范围长度
            if (strlengthMeg != undefined) {
                $(this).rules("add",
                    {
                        rangelength: [strlengthMin,strlengthMax],
                        messages: {
                            rangelength: strlengthMeg
                        }
                    });
            }

            //字符串必填
            if (requidMeg != undefined && requidMeg!=="") {
                $(this).rules("add",
                    {
                        required: true,
                        messages: {
                            required: requidMeg
                        }
                    });
            }

            //邮箱验证
            if (mailMeg != undefined) {
                $(this).rules("add",
                    {
                        email: true,
                        messages: {
                            email: mailMeg
                        }
                    });
            }

            //范围验证
            if (rangeMeg != undefined) {
                $(this).rules("add",
                    {
                        range: [rangeMin,rangeMax],
                        messages: {
                            range: rangeMeg
                        }
                    });
            }

            //正则表达式验证
            if (regexMeg != undefined) {
                $(this).rules("add",
                    {
                        regex: [new RegExp(regex)],
                        messages: {
                            regex: regexMeg
                        }
                    });
            }
        }
    });

})(jQuery);